/*
 *  main.cpp
 *  OfflineTranslator
 *
 *  Created by PH on 5.9.10.
 *  Copyright 2010 Petr Homola. All rights reserved.
 *
 */

// A simple example how to use the parser

#include "stdafx.h"
#include "Grammar.h"
#ifdef TR1
#include <tr1/memory>
#else
#include <memory>
#endif

using namespace nlp;

int main(int argc, char* argv[]) {
    Grammar grammar;
	PhraseTable chart;
	
	// Adding AVMs to the chart
	// (we're adding the phrase "new book" where the word "book" is ambiguous - the POS is "noun" and "verb"
	
	AVM* avm1 = new AVM();
	avm1->put("pos", "adj");
	avm1->put("form", "new");
	avm1->put("order", "0");
	chart.add(new Phrase(shared_ptr<AVM>(avm1), 0, 1));

	AVM* avm2 = new AVM();
	avm2->put("pos", "noun");
	avm2->put("form", "book");
	avm2->put("order", "1");
	chart.add(new Phrase(shared_ptr<AVM>(avm2), 1, 2));
	
	AVM* avm3 = new AVM();
	avm3->put("pos", "verb");
	avm3->put("form", "book");
	avm3->put("order", "1");
	chart.add(new Phrase(shared_ptr<AVM>(avm3), 1, 2));
	
	// Adding a rule that combines an adjective followed by a noun to a new AVM (complex NP)
	
	grammar.add(new Rule(BLOCKRET(AVM*)(vector<Phrase*>* phrases) BLOCKBODY(AVM*) {
		if (phrases->size() == 2) {
			shared_ptr<AVM> avm1 = (*phrases)[0]->getAvm();
			shared_ptr<AVM> avm2 = (*phrases)[1]->getAvm();
			if (avm1->get("pos").getString() == "adj" && avm2->get("pos").getString() == "noun") {
				AVM* newAvm = new AVM(avm2);
				newAvm->put("adj", avm1);
				return newAvm;
			}
		}
		return NULL;
	})); /**/
	
	// Applying the grammar to the chart
	
	grammar.apply(&chart);
	
	// Enumerating through the chart; we want only 'short' paths (that consist of complex AVMs)
	
	chart.enumerateShortestPaths(BLOCK()(PhrasePath* path) {
		cout << path->description() << endl;
	});
	
    return 0;
}
